<template>
    <el-row>
        <el-col :span="24">
            <el-form :inline="true"
                :model="newsSearchObj"
                class="search_prod tl">
                <el-form-item label="">
                    <el-cascader :options="data"
                        :props="optionProps"
                        :show-all-levels="false"
                        v-model="op"
                        size="small"
                        change-on-select
                        placeholder="新闻分类"
                        clearable></el-cascader>
                </el-form-item>
                <el-form-item label="">
                    <el-input size="small"
                        clearable
                        v-model="newsSearchObj.title"
                        placeholder="新闻标题"></el-input>
                </el-form-item>
                <el-form-item label=""
                    class="time_width">
                    <el-date-picker v-model="createTime"
                        type="datetimerange"
                        size="small"
                        align="right"
                        unlink-panels
                        value-format="timestamp"
                        range-separator="-"
                        clearable
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :picker-options="pickerOptions2">
                    </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-checkbox true-label="1"
                        false-label=''
                        v-model="newsSearchObj.is_top">是否置顶</el-checkbox>
                    <el-checkbox true-label="1"
                        false-label=''
                        v-model="newsSearchObj.show">是否显示</el-checkbox>
                </el-form-item>
                <el-form-item>
                    <el-row class="tl">
                        <el-col :span="24">
                            <el-button :loading='$store.state.isLoading'
                                size="small"
                                class="top_weiy"
                                type="primary"
                                @click="onSubmit">查询</el-button>
                            <el-button :loading='$store.state.isLoading'
                                size="small"
                                class="top_weiy"
                                type="primary"
                                @click="delSubmit">一键清除</el-button>
                        </el-col>
                    </el-row>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>
<script>
export default {
  name: "productSearch",
  data() {
    return {
      pickerOptions2: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      newsSearchObj: {
        cate_id: "",
        title: "",
        created_time_begin: "",
        created_time_end: "",
        is_top: "",
        show: ""
      },
      createTime: [],
      optionProps: {
        value: "id",
        label: "title",
        children: "z"
      },
      data: [],
      op: []
    };
  },
  methods: {
    delSubmit() {
      this.newsSearchObj = {
        cate_id: "",
        title: "",
        created_time_begin: "",
        created_time_end: "",
        is_top: "",
        show: ""
      };
      this.$emit("sendSearch", this.newsSearchObj);
    },
    onSubmit() {
      console.log(this.newsSearchObj);
      this.$emit("sendSearch", this.newsSearchObj);
    },
    getNewFenLeiTree() {
      var params = {};
      params.parent_id = "0";
      this.$api.getNewsFenlei.getNewsList(params).then(res => {
        if (res.data.code == 200 && res.data.status == 1 && res.data.result) {
          this.data = res.data.result;
        }
      });
    }
  },
  created() {
    this.getNewFenLeiTree();
  },
  watch: {
    op(val) {
      this.newsSearchObj.cate_id = val[val.length - 1];
    },
    createTime(val) {
      if (val != null) {
        this.newsSearchObj.created_time_begin = this.$fnc.phptime(val[0]);
        this.newsSearchObj.created_time_end = this.$fnc.phptime(val[1]);
      } else {
        this.newsSearchObj.created_time_begin = "";
        this.newsSearchObj.created_time_end = "";
      }
    }
  }
};
</script>
